const app = getApp();
const https = require('../../utils/https');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    check_question_list: [],
    check_point_result_id: 0,
    page: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this,
      is_login_inspector = wx.getStorageSync('is_login_inspector');
    if (!is_login_inspector || is_login_inspector != 1) {
      wx.showToast({
        title: '请先登陆验房师账号',
        icon: 'none',
        duration: 1500
      })
      wx.navigateBack({
        delta: 0,
      })
      return false;
    }
    if (options && options.check_point_result_id != '') {
      that.setData({
        check_point_result_id: options.check_point_result_id,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      check_question_list: [],
      page: 0
    })
    this.getCheckQuestionList();
  },
  /**
   * @xk 获取验房点位的问题
   */
  getCheckQuestionList: function () {
    var that = this,
      check_question_list = this.data.check_question_list,
      page = this.data.page,
      check_point_result_id = this.data.check_point_result_id;
    https.post('getCheckQuestionList', {
      check_point_result_id: check_point_result_id,
      page: page
    }, false, '加载中').then(res => {
      if (res.list) {
        check_question_list.push.apply(check_question_list, res.list);
      }
      that.setData({
        check_question_list: check_question_list,
        page: res.page
      })
    })
  },
  /**
   * @xk 录入问题
   */
  inputquestion: function () {
    var check_point_result_id = this.data.check_point_result_id;
    wx.navigateTo({
      url: '/pages/inspection_input/input?check_point_result_id=' + check_point_result_id,
    })
  },
  editQuestion: function (obj) {
    var check_point_result_id = this.data.check_point_result_id,
      id = obj.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/inspection_input/input?check_point_result_id=' + check_point_result_id + '&id=' + id,
    })
  },

  delQuestion: function (obj) {
    var that = this;
    var id = obj.currentTarget.dataset.id;
    wx.showModal({
      title: '问题删除确认',
      content: '您确定要删除这一问题吗？',
      success: function (sm) {
        if (sm.confirm) {
          https.post('deleteQuestion', {
            id: id
          }, false, '正在执行删除').then(res => {
            that.setData({
              check_question_list: [],
              page: 0
            })
            that.getCheckQuestionList();
          })
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      check_question_list: [],
      page: 0
    })
    this.onShow();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getCheckQuestionList();
  },
})